const app = getApp();
Page({

    // 页面的初始数据
    data: {
        iftanchuan:false,
        goodslist:[],
        province:'',
        city:'',
        area:'',
        text:'',
        name:'',
        phone:'',
        textarea:'',
        location:{},
        is_buy:''
    },

    // 页面的生命周期函数 – 监听页面加载
    onLoad(res) {
        var goodslist = JSON.parse(res.goods);
        var total = res.total;
        console.log(res,goodslist)
        this.setData({
            goods:res.goods,
            goodslist,
            total,
            is_buy:res.is_buy?res.is_buy:''
        })
        this.getlocationData()
    },

    input_name(e){
        this.setData({
            name:e.detail.value
        })
    },

    input_phone(e){
        this.setData({
            phone:e.detail.value
        })
    },

    input_area(e){
        this.setData({
            textarea:e.detail.value
        })
    },

    open_tanchuang(){
        this.setData({
            iftanchuan:true
        })
        this.selectComponent('#addsel').setdefaul_val(this.data.text)
    },

    close_tanchuang(){
        this.setData({
            iftanchuan:false
        })
    },

    go2pay(){
        var goodslist = this.data.goodslist;
        var goods = {};
        var idxs = 0;
        for(var i of goodslist){
            goods[idxs] = i
            idxs++
        }
        var total = this.data.total;
        var address = {};
        address.name = this.data.location.name;
        address.tel = this.data.location.phone;
        address.address  = this.data.location.province + this.data.location.city + this.data.location.area + this.data.location.textarea
        console.log(this.data.is_buy)
        if(this.data.is_buy!=''){
            swan.request({
                url: app.globalData.urlapi + 'add2Order',
                method:'POST',
                header: {'content-type': 'application/x-www-form-urlencoded'},
                data:{
                    mid:app.globalData.userInfo.mid,
                    goods:JSON.stringify(goods),
                    address:JSON.stringify(address),
                    total,
                    is_buy:this.data.is_buy
                },
                success(e){
                    console.log(e)
                    if(e.data.code == 1){
                        swan.navigateTo({
                            url: '../order_success/order_success?order_id='+e.data.order.order_id
                        });
                    }else{
                        swan.showToast({
                            title: e.data.msg,
                            icon:'loading',
                            duration:'800'
                        });
                    }
                }
            });
        }else{
            swan.request({
                url: app.globalData.urlapi + 'add2Order',
                method:'POST',
                header: {'content-type': 'application/x-www-form-urlencoded'},
                data:{
                    mid:app.globalData.userInfo.mid,
                    goods:JSON.stringify(goods),
                    address:JSON.stringify(address),
                    total,
                },
                success(e){
                    console.log(e)
                    if(e.data.code == 1){
                        swan.navigateTo({
                            url: '../order_success/order_success?order_id='+e.data.order.order_id
                        });
                    }else{
                        swan.showToast({
                            title: e.data.msg,
                            icon:'loading',
                            duration:'800'
                        });
                    }
                }
            });
        }
    },

    getlocationData(){
        const that = this;
        swan.request({
            url: app.globalData.urlapi + 'listAddress',
            data:{
                mid:app.globalData.userInfo.mid,
            },
            success(e){
                var obj = e.data.find(r=>r.status==1);
                var location={
                    name:obj.name,
                    phone:obj.tel,
                    textarea:obj.address,
                    province:obj.province,
                    city:obj.city,
                    area:obj.area,
                    text:obj.province+'-'+obj.city+'-'+obj.area,
                }
                console.log(location)
                that.setData({
                    location,
                    province:obj.province,
                    city:obj.city,
                    area:obj.area,
                    text:obj.province+'-'+obj.city+'-'+obj.area,
                    name:obj.name,
                    phone:obj.tel,
                    textarea:obj.address
                })
            }
        });
    },

    sure(){
        var location={
            name:this.data.name,
            phone:this.data.phone,
            textarea:this.data.textarea,
            province:this.data.province,
            city:this.data.city,
            area:this.data.area,
            text:this.data.province+'-'+this.data.city+'-'+this.data.area,
        }
        this.setData({
            location
        })
        this.close_tanchuang()
    },

    save(){
        const that = this;
        swan.request({
            url: app.globalData.urlapi + 'addAddress',
            data:{
                mid:app.globalData.userInfo.mid,
                name:this.data.name,
                tel:this.data.phone,
                address:this.data.textarea,
                province:this.data.province,
                city:this.data.city,
                area:this.data.area,
            },
            success(e){
                swan.showToast({
                    title: e.data.msg,
                    duration:'800'
                });
            }
        });
    },

    getdata(data){  //获取子组件数据
        console.log(data)
        this.setData({
           province:data.detail.province,
           city:data.detail.city,
           area:data.detail.area
        })
    },

    // 页面的生命周期函数 – 监听页面初次渲染完成
    onReady(res) {

    },

    // 页面的生命周期函数 – 监听页面显示
    onShow(res) {

    },

    // 页面的生命周期函数 – 监听页面隐藏
    onHide(res) {

    },

    // 页面的生命周期函数 – 监听页面卸载
    onUnload(res) {

    },

    // 页面的生命周期函数 – 监听页面重启，单击重启按钮时触发
    onForceReLaunch(res) {

    },

    // 页面的事件处理函数 – 监听用户下拉动作
    onPullDownRefresh(res) {

    },

    // 页面的事件处理函数 – 上拉触底事件的处理函数
    onReachBottom(res) {

    },

    // 页面的事件处理函数 – 用户点击右上角转发
    onShareAppMessage(res) {

    },

    // 页面的事件处理函数 – 页面滚动触发事件的处理函数
    onPageScroll(res) {

    },

    // 页面的事件处理函数 – 当前是 tab 页时，点击 tab 时触发
    onTabItemTap(res) {

    }
});